package com.tosit.bmx.controller.admin;

import com.tosit.bmx.common.json.JSONModel;
import com.tosit.bmx.common.json.JSONUtil;
import com.tosit.bmx.domian.Usershow;
import com.tosit.bmx.service.admin.WebManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.jws.WebService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 会员展示管理类
 *
 * @author tsl
 * @create 2018-04-26 17:01
 **/
@Controller
@RequestMapping
public class UserShowController {
    @Autowired
    WebManagerService webManagerService;

    /**
     * 获取所有会员展示
     *
     * @param
     * @return
     * @author tsl
     * @date 2018/4/26 17:24
     */
    @RequestMapping(value = "/usershow", method = RequestMethod.GET)
    public void gainUserShowAll(HttpServletResponse response) {
        List<Usershow> result = webManagerService.gainUserShowAll();
        if (result != null) {
            JSONModel.put("data", result);
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }


    /**
     * 添加会员展示
     *
     * @param
     * @return
     * @author tsl
     * @date 2018/4/26 17:24
     */
    @RequestMapping(value = "/usershow", method = RequestMethod.POST)
    public void addUserShow(Usershow usershow, @RequestParam("file")MultipartFile file, HttpServletResponse response,HttpServletRequest request) {
        Integer result = null;
        String ctime = "" + System.currentTimeMillis()/1000;
        if (!file.isEmpty()) {
            //上传文件路径
            String path = request.getServletContext().getRealPath("/upload/usershow/");
            //上传文件名
            String filename = file.getOriginalFilename();
            String newFileName = (int)(Math.random()*10000000) +""+(int)(Math.random()*10000000)+
                    filename.substring(filename.lastIndexOf("."));
            try {
                file.transferTo(new File(path + File.separator + newFileName));
                usershow.setImgpath("/upload/usershow/"+newFileName);
                usershow.setCtime(ctime.trim());
                result= webManagerService.addUserShow(usershow);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

    /**
     * 删除会员展示
     *
     * @param ids
     * @return
     * @author tsl
     * @date 2018/4/26 17:24
     */
    @RequestMapping(value = "/usershow", method = RequestMethod.DELETE)
    public void addUserShow(@RequestBody List<Integer> ids, HttpServletResponse response) {
        Integer result = webManagerService.removeUserShows(ids);
        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }

    /**
     * 更新会员展示信息
     *
     * @param
     * @return
     * @author tsl
     * @date 2018/4/26 17:24
     */
    @RequestMapping(value = "/usershow/fix", method = RequestMethod.POST)
    public void modifyUserShow(Usershow usershow, @RequestParam("file")MultipartFile file, HttpServletResponse response,HttpServletRequest request) {
        Integer result = null;
        if (!file.isEmpty()) {
            //上传文件路径
            String path = request.getServletContext().getRealPath("/upload/usershow/");
            //上传文件名
            String filename = file.getOriginalFilename();
            String newFileName = (int)(Math.random()*10000000) +""+(int)(Math.random()*10000000)+
                    filename.substring(filename.lastIndexOf("."));
            try {
                file.transferTo(new File(path + File.separator + newFileName));
                usershow.setImgpath("/upload/usershow/"+newFileName);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        result= webManagerService.modifyUserShow(usershow);
        if (result != null) {
            JSONModel.put("status", "success");
        } else {
            JSONModel.put("status", "fail");
        }
        JSONUtil.printJsonModel(response);
    }


}
